package com.sunday.common.netty.e3_Pojo.server;

import com.sunday.common.netty.e3_Pojo.UnixTime;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TimeServerHandler extends ChannelInboundHandlerAdapter {

    /**
     * channelActive 方法将会在连接被建立并且准备进行通信时被调用。
     */
    @Override
    public void channelActive(final ChannelHandlerContext ctx) {
        log.info("channelActive 方法将会在连接被建立并且准备进行通信时被调用。");
        ChannelFuture f = ctx.writeAndFlush(new UnixTime());
        f.addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

}
